AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.vent = 0
	self.damaged = 0
	LS_RegisterEnt(self.Entity)

	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "Vent Container" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end
	
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "Vent Container") then
		if (value ~= 0) then
			if ( self.vent == 0 ) then
				self.vent = 1
			end
		else
			if ( self.vent == 1 ) then
				self.vent = 0
			end
		end
	end
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.Entity:SetColor(255, 255, 255, 255)
	self.health = self.max_health
	self.damaged = 0
end

function ENT:Destruct()
	local Effect = EffectData()
		Effect:SetOrigin(self.Entity:GetPos())
		Effect:SetScale(1)
		Effect:SetMagnitude(25)
		util.Effect("Explosion", Effect, true, true)
	doempspark(self.Entity:GetPos()+Vector(0,0,50),10)
	doempspark(self.Entity:GetPos(),10)
	doempspark(self.Entity:GetPos()+Vector(0,50,0),10)
	doempspark(self.Entity:GetPos()+Vector(50,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(-50,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,-50,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,0,-50),10)
	doempspark(self.Entity:GetPos()+Vector(0,0,50),10)


	doempspark(self.Entity:GetPos()+Vector(0,300,0),10)
	doempspark(self.Entity:GetPos()+Vector(300,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(-300,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,-300,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,0,-300),10)

	doempspark(self.Entity:GetPos()+Vector(0,500,0),10)
	doempspark(self.Entity:GetPos()+Vector(500,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(-500,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,-500,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,0,-500),10)

	doempspark(self.Entity:GetPos()+Vector(0,800,0),10)
	doempspark(self.Entity:GetPos()+Vector(800,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(-800,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,-800,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,0,-800),10)

	doempspark(self.Entity:GetPos()+Vector(0,1000,0),10)
	doempspark(self.Entity:GetPos()+Vector(1000,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(-1000,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,-1000,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,0,-1000),10)

	doempspark(self.Entity:GetPos()+Vector(0,1500,0),10)
	doempspark(self.Entity:GetPos()+Vector(1500,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(-1500,0,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,-1500,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,0,-1500),10)
	
	doempspark(self.Entity:GetPos()+Vector(0,1500,1500),10)
	doempspark(self.Entity:GetPos()+Vector(1500,1500,0),10)
	doempspark(self.Entity:GetPos()+Vector(1500,0,1500),10)
	doempspark(self.Entity:GetPos()+Vector(-1500,-1500,0),10)
	doempspark(self.Entity:GetPos()+Vector(0,-1500,0),10)
	doempspark(self.Entity:GetPos()+Vector(-1500,0,-1500),10)
	doempspark(self.Entity:GetPos()+Vector(0,-1500,-1500),10)
	if RD_GetResourceAmount(self, "energy") > 0 then
		for k,v in pairs(ents.FindInSphere(self.Entity:GetPos(),math.Round(RD_GetResourceAmount(self, "energy")))) do --emp shockwave
			empattack(self.Entity:GetVar("Owner"),v,math.Round(math.sqrt(RD_GetResourceAmount(self, "energy"))))
			--ohshit
		end
		local ball = ents.Create("prop_combine_ball")
		ball:SetPos(self.Entity:GetPos())
		ball:Fire("explode", "", 0) 
	end
	
	self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
end




function ENT:Output()
	return 1
end


function ENT:ShowOutput(value)
	self:SetOverlayText( "Energy\n(" .. value .. ")" )
	return value
end


function ENT:Think()
	self:ShowOutput(self.val1)
	if ( self.timer > CurTime() ) then return end
	
	local energy = RD_GetResourceAmount(self, "energy")
	self.val1 = energy
	
	if self.vent == 1 then --MAYDAY! VENT THE CONTAINER! (lol)
		--you can only vent in water
		local entpos = self.Entity:GetPos()
		if (self.environment.inwater == 1) then
			RD_ConsumeResource(self, "energy", self.val1);
			local pos = (self.Entity:GetPos() + (self.Entity:GetAngles():Up() * self.Entity:BoundingRadius()))
			zapme(pos, 1)
			local tAllPlayers = player.GetAll()
			NextUpdateTime = CurTime() + 1
			for _, pPlayer in pairs( tAllPlayers ) do
				if (pPlayer.suit.inwater == 1) then
					if (self.Entity:GetPos():Distance(pPlayer.Entity:GetPos()) < 600) then
						local pos2 = (pPlayer.Entity:GetPos() + (pPlayer.Entity:GetAngles():Forward() * 10))
						zapme(pos2, 1)
						pPlayer.Entity:TakeDamage( (energy / 100), 0 )
					end
				end
			end
			self:Destruct() --terminal damage by water
		end
	end
	
	if ((self.damaged == 1) and (energy > 0)) then
		local entpos = self.Entity:GetPos()
		if (self.environment.inwater == 1) then
			local pos = (self.Entity:GetPos() + (self.Entity:GetAngles():Up() * self.Entity:BoundingRadius()))
			zapme(pos, 1)
			local tAllPlayers = player.GetAll()
			NextUpdateTime = CurTime() + 1
			for _, pPlayer in pairs( tAllPlayers ) do
				if (pPlayer.suit.inwater == 1) then
					if (self.Entity:GetPos():Distance(pPlayer.Entity:GetPos()) < 600) then
						local pos2 = (pPlayer.Entity:GetPos() + (pPlayer.Entity:GetAngles():Forward() * 10))
						zapme(pos2, 1)
						pPlayer.Entity:TakeDamage( (energy / 100), 0 )
					end
				end
			end
			max_energy = RD_GetUnitCapacity(self, "energy")
			if (energy >= max_energy) then
				RD_ConsumeResource(self, "energy", max_energy)
			else
				RD_ConsumeResource(self, "energy", energy)
			end
		else
			if (math.random(1, 10) < 2) then
				local pos = (self.Entity:GetPos() + (self.Entity:GetAngles():Up() * self.Entity:BoundingRadius()))
				zapme(pos, 1)
				local dec = math.random(200, 2000)
				if (energy > dec) then
					RD_ConsumeResource(self, "energy", dec)
				else
					RD_ConsumeResource(self, "energy", energy)
				end
			end
		end
	end
	
	if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", energy) end
	self.timer = CurTime() +  1
end

function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
